Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TableGen] Simplify generated code for isSubclass #117351

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jayfoad
Copy link
Contributor

@jayfoad jayfoad commented Nov 22, 2024

Implement isSubclass with some tables and a single call to lower_bound
instead of nested switches.

Part of the motivation for this is improving compile time when clang-18
is used as a host compiler, since it seems to have trouble with very
large switch statements.

Implement isSubclass with some tables and a single call to lower_bound
instead of nested switches.

Part of the motivation for this is improving compile time when clang-18
is used as a host compiler, since it seems to have trouble with very
large switch statements.
@llvmbot
Copy link
Member

llvmbot commented Nov 22, 2024

@llvm/pr-subscribers-tablegen

Author: Jay Foad (jayfoad)

Changes

Implement isSubclass with some tables and a single call to lower_bound
instead of nested switches.

Part of the motivation for this is improving compile time when clang-18
is used as a host compiler, since it seems to have trouble with very
large switch statements.


Full diff: https://github.com/llvm/llvm-project/pull/117351.diff

1 Files Affected:

  • (modified) llvm/utils/TableGen/AsmMatcherEmitter.cpp (+18-32)
diff --git a/llvm/utils/TableGen/AsmMatcherEmitter.cpp b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
index ade393c11b7a24..68e9b434312a80 100644
--- a/llvm/utils/TableGen/AsmMatcherEmitter.cpp
+++ b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
@@ -2541,7 +2541,6 @@ static void emitIsSubclass(CodeGenTarget &Target,
   OS << "  if (A == B)\n";
   OS << "    return true;\n\n";
 
-  bool EmittedSwitch = false;
   for (const auto &A : Infos) {
     std::vector<StringRef> SuperClasses;
     if (A.IsOptional)
@@ -2551,42 +2550,29 @@ static void emitIsSubclass(CodeGenTarget &Target,
         SuperClasses.push_back(B.Name);
     }
 
-    if (SuperClasses.empty())
+    if (SuperClasses.empty()) {
+      OS << "  static constexpr ArrayRef<uint16_t> " << A.Name << "_SuperClasses;\n";
       continue;
-
-    // If this is the first SuperClass, emit the switch header.
-    if (!EmittedSwitch) {
-      OS << "  switch (A) {\n";
-      OS << "  default:\n";
-      OS << "    return false;\n";
-      EmittedSwitch = true;
     }
 
-    OS << "\n  case " << A.Name << ":\n";
-
-    if (SuperClasses.size() == 1) {
-      OS << "    return B == " << SuperClasses.back() << ";\n";
-      continue;
-    }
-
-    if (!SuperClasses.empty()) {
-      OS << "    switch (B) {\n";
-      OS << "    default: return false;\n";
-      for (StringRef SC : SuperClasses)
-        OS << "    case " << SC << ": return true;\n";
-      OS << "    }\n";
-    } else {
-      // No case statement to emit
-      OS << "    return false;\n";
-    }
+    OS << "  static constexpr uint16_t " << A.Name << "_SuperClasses[] = {";
+    ListSeparator LS;
+    for (auto &SC : SuperClasses)
+      OS << LS << SC;
+    OS << "};\n";
   }
+  OS << "\n";
 
-  // If there were case statements emitted into the string stream write the
-  // default.
-  if (EmittedSwitch)
-    OS << "  }\n";
-  else
-    OS << "  return false;\n";
+  OS << "  static constexpr ArrayRef<uint16_t> SuperClassTable[] = {\n";
+  OS << "    {}, // InvalidMatchClass\n";
+  OS << "    {}, // OptionalMatchClass\n";
+  for (const auto &A : Infos)
+    OS << "    " << A.Name << "_SuperClasses,\n";
+  OS << "  };\n\n";
+
+  OS << "  ArrayRef<uint16_t> SuperClasses = SuperClassTable[(unsigned)A];\n";
+  OS << "  const uint16_t *It = lower_bound(SuperClasses, (unsigned)B);\n";
+  OS << "  return It != SuperClasses.end() && *It == (unsigned)B;\n";
 
   OS << "}\n\n";
 }

Copy link

github-actions bot commented Nov 22, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@michaelmaitland
Copy link
Contributor

IIUC we could have two nested switches (constant time lookup), but now we are using binary search (logarithmic time lookup). Will this cause any time related regressions?

@jayfoad
Copy link
Contributor Author

jayfoad commented Nov 26, 2024

IIUC we could have two nested switches (constant time lookup), but now we are using binary search (logarithmic time lookup). Will this cause any time related regressions?

Good question. I tried measuring this with check-llvm-mc but the run-to-run variation was pretty high. I'll see if I can detect any difference in a single execution of llvm-mc.

@jayfoad
Copy link
Contributor Author

jayfoad commented Nov 26, 2024

IIUC we could have two nested switches (constant time lookup), but now we are using binary search (logarithmic time lookup). Will this cause any time related regressions?

Good question. I tried measuring this with check-llvm-mc but the run-to-run variation was pretty high. I'll see if I can detect any difference in a single execution of llvm-mc.

I think I'm seeing around 1% slowdown in some cases, which is not great. Part of the difference is that isSubclass is now simple enough to be inlined, which might affect performance regardless of what algorithm is used inside isSubclass.

Here's an example I used for testing:

perf stat -e cycles -r 300 llvm-mc -triple x86_64-unknown-unknown test/MC/X86/I86-64.s -filetype=null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants